import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_app/ui/welcome/welcome_view.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  initialize(); //初始化
  runApp(MyApp());
}
/// 初始化
void initialize() {
  /// bruno主题配置
  BrnInitializer.register(
    allThemeConfig: BrnAllThemeConfig(
      // 全局配置
      commonConfig: BrnCommonConfig(),
      // dialog配置
      dialogConfig: BrnDialogConfig(),
    ),
  );
}

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey();

//继承 无状态的组件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 使用屏幕适配
    return ScreenUtilInit(
        // UI设计图大小
        designSize: Size(375, 667),
        builder: (BuildContext context, Widget? child) => GetMaterialApp(
              title: '效能监督',
              // 本地化 设置语言
              localizationsDelegates: [
                // pull_to_refresh 库 语言初始化
                RefreshLocalizations.delegate,
                // 为Material Components库提供了本地化的字符串和其他值
                GlobalMaterialLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
                // 定义widget默认的文本方向，从左到右或从右到左。
                GlobalWidgetsLocalizations.delegate,
              ],
              supportedLocales: [
                const Locale('zh', 'CN'), // 中国
                const Locale('en', 'US'), // English
                // ... other locales the app supports
              ],
              theme: ThemeData(
                primarySwatch: Colors.red,
                visualDensity: VisualDensity.adaptivePlatformDensity,
              ),
              // theme: _buildShrineTheme(),
              // 注册 NavigatorState
              navigatorKey: navigatorKey,
              home: WelcomePage(title: "欢迎页面"),
              builder: (BuildContext context, Widget? child) {
                //add this line
                ScreenUtil.registerToBuild(context);
                // 初始化 loading
                final easyLoading = EasyLoading.init();
                child = MediaQuery(
                  //Setting font does not change with system font size
                  data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                  child: child!,
                );
                return easyLoading(context, child);
              },
            ));
  }
}

ThemeData _buildShrineTheme() {
  final ThemeData base = ThemeData.light();
  return base.copyWith(
    colorScheme: _shrineColorScheme,
    textTheme: _buildShrineTextTheme(base.textTheme),
  );
}

TextTheme _buildShrineTextTheme(TextTheme base) {
  return base
      .copyWith(
        caption: base.caption!.copyWith(
            fontWeight: FontWeight.w400,
            fontSize: 14,
            letterSpacing: defaultLetterSpacing),
        button: base.button!.copyWith(
            fontWeight: FontWeight.w500,
            fontSize: 14,
            letterSpacing: defaultLetterSpacing),
      )
      .apply(
          fontFamily: 'Rubik',
          displayColor: shrineBrown900,
          bodyColor: shrineBrown900);
}

const ColorScheme _shrineColorScheme = ColorScheme(
  primary: shrinePink100,
  primaryVariant: shrineBrown900,
  secondary: shrinePink50,
  secondaryVariant: shrineBrown900,
  surface: shrineSurfaceWhite,
  background: shrineBackgroundWhite,
  error: shrineErrorRed,
  onPrimary: shrineBrown900,
  onSecondary: shrineBrown900,
  onSurface: shrineBrown900,
  onBackground: shrineBrown900,
  onError: shrineSurfaceWhite,
  brightness: Brightness.light,
);

const Color shrinePink50 = Color(0xFFFEEAE6);
const Color shrinePink100 = Color(0xFFFEDBD0);
const Color shrinePink300 = Color(0xFFFBB8AC);
const Color shrinePink400 = Color(0xFFEAA4A4);

const Color shrineBrown900 = Color(0xFF442B2D);
const Color shrineBrown600 = Color(0xFF7D4F52);

const Color shrineErrorRed = Color(0xFFC5032B);

const Color shrineSurfaceWhite = Color(0xFFFFFBFA);
const Color shrineBackgroundWhite = Colors.white;

const defaultLetterSpacing = 0.03;
